package com.eco.controller;

import com.alibaba.fastjson.JSONObject;
import com.eco.annotation.AuthToken;
import com.eco.netty.NettyClient;
import com.eco.utils.NettyCtxMap;
import io.netty.channel.ChannelHandlerContext;
import java.io.UnsupportedEncodingException;
import java.time.Duration;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.ThreadUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.tags.BindTag;

@RestController
@RequestMapping("ir")
public class LightController {
    private static final Logger log = LoggerFactory.getLogger((Class<?>) LightController.class);
    private static Map<String, String> orderMap = new ConcurrentHashMap();

    @Resource
    private NettyClient nettyClient;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    static {
        orderMap.put("inon1", "00 01 00 00 00 06 FF 05 00 64 FF 00");
        orderMap.put("inon2", "00 01 00 00 00 06 FF 05 00 65 FF 00");
        orderMap.put("inon3", "00 01 00 00 00 06 FF 05 00 66 FF 00");
        orderMap.put("inon4", "00 01 00 00 00 06 FF 05 00 67 FF 00");
        orderMap.put("inon5", "00 01 00 00 00 06 FF 05 00 68 FF 00");
        orderMap.put("inon6", "00 01 00 00 00 06 FF 05 00 69 FF 00");
        orderMap.put("inon7", "00 01 00 00 00 06 FF 05 00 6A FF 00");
        orderMap.put("inon8", "00 01 00 00 00 06 FF 05 00 6B FF 00");
        orderMap.put("outon1", "00 01 00 00 00 06 FF 05 00 64 FF 00");
        orderMap.put("inoff1", "00 01 00 00 00 06 FF 05 00 64 00 00");
        orderMap.put("inoff2", "00 01 00 00 00 06 FF 05 00 65 00 00");
        orderMap.put("inoff3", "00 01 00 00 00 06 FF 05 00 66 00 00");
        orderMap.put("inoff4", "00 01 00 00 00 06 FF 05 00 67 00 00");
        orderMap.put("inoff5", "00 01 00 00 00 06 FF 05 00 68 00 00");
        orderMap.put("inoff6", "00 01 00 00 00 06 FF 05 00 69 00 00");
        orderMap.put("inoff7", "00 01 00 00 00 06 FF 05 00 6A 00 00");
        orderMap.put("inoff8", "00 01 00 00 00 06 FF 05 00 6B 00 00");
        orderMap.put("outoff1", "00 01 00 00 00 06 FF 05 00 64 00 00");
    }

    public boolean sendCommand(String clientId, String order) {
        String command = orderMap.get(clientId + order);
        try {
            if (StringUtils.isEmpty(clientId) || StringUtils.isEmpty(command)) {
                log.error("clientId or command is null");
                return false;
            }
            ChannelHandlerContext ctx = NettyCtxMap.get(clientId);
            if (ctx == null) {
                log.error("ChannelHandlerContext is null");
                return false;
            }
            String msg = new String(command.getBytes(), "utf-8");
            ctx.writeAndFlush(msg);
            log.info("向继电器[{}]发送指令[{}]成功", clientId, msg);
            if (StringUtils.contains(order, "on")) {
                String offset = StringUtils.replace(order, "on", "");
                this.stringRedisTemplate.opsForValue().setBit(clientId, Long.parseLong(offset), true);
                return true;
            }
            if (StringUtils.contains(order, "off")) {
                String offset2 = StringUtils.replace(order, "off", "");
                this.stringRedisTemplate.opsForValue().setBit(clientId, Long.parseLong(offset2), false);
                return true;
            }
            return true;
        } catch (UnsupportedEncodingException e) {
            log.error(e.toString());
            return false;
        }
    }

    @RequestMapping({"/setWaterLevel"})
    public JSONObject setWaterLevel(String waterLevel) {
        JSONObject result = new JSONObject();
        String waterLevel2 = StringUtils.trim(waterLevel);
        if (StringUtils.isEmpty(waterLevel2) || !NumberUtils.isCreatable(waterLevel2)) {
            log.error("水位阈值参数为空或不是数字");
            result.put("msg", (Object) "水位阈值参数为空或不是数字");
            result.put(BindTag.STATUS_VARIABLE_NAME, (Object) false);
            return result;
        }
        if (StringUtils.equals("0", waterLevel2)) {
            this.stringRedisTemplate.opsForValue().setBit("autoStatus", 1L, false);
            log.info("关闭自动控制诱鱼灯");
        } else {
            this.stringRedisTemplate.opsForValue().setBit("autoStatus", 1L, true);
            log.info("成功设置水位阈值为:" + waterLevel2);
        }
        this.stringRedisTemplate.opsForValue().set("waterLevel", waterLevel2);
        result.put("msg", (Object) ("成功设置水位阈值:" + waterLevel2));
        result.put(BindTag.STATUS_VARIABLE_NAME, (Object) true);
        return result;
    }

    @RequestMapping({"/getWaterLevel"})
    public JSONObject getWaterLevel() {
        JSONObject result = new JSONObject();
        result.put(BindTag.STATUS_VARIABLE_NAME, (Object) true);
        result.put("waterLevel", (Object) this.stringRedisTemplate.opsForValue().get("waterLevel"));
        return result;
    }

    @RequestMapping({"/getLampStatus"})
    public JSONObject getLampStatus() {
        JSONObject result = new JSONObject();
        result.put("sitein1", (Object) this.stringRedisTemplate.opsForValue().getBit("in", 1L));
        result.put("sitein2", (Object) this.stringRedisTemplate.opsForValue().getBit("in", 2L));
        result.put("sitein3", (Object) this.stringRedisTemplate.opsForValue().getBit("in", 3L));
        result.put("sitein4", (Object) this.stringRedisTemplate.opsForValue().getBit("in", 4L));
        result.put("sitein5", (Object) this.stringRedisTemplate.opsForValue().getBit("in", 5L));
        result.put("sitein6", (Object) this.stringRedisTemplate.opsForValue().getBit("in", 6L));
        result.put("sitein7", (Object) this.stringRedisTemplate.opsForValue().getBit("in", 7L));
        result.put("sitein8", (Object) this.stringRedisTemplate.opsForValue().getBit("in", 8L));
        result.put("siteout1", (Object) this.stringRedisTemplate.opsForValue().getBit("out", 1L));
        return result;
    }

    @RequestMapping({"/order2Knd"})
    public JSONObject order2Knd(String site, String order) {
        JSONObject result = new JSONObject();
        String site2 = StringUtils.trim(site);
        String order2 = StringUtils.trim(order);
        if (StringUtils.isEmpty(site2) || StringUtils.isEmpty(order2)) {
            log.error("点位或指令参数为空");
            result.put("msg", (Object) "点位或指令参数为空");
            result.put(BindTag.STATUS_VARIABLE_NAME, (Object) false);
            return result;
        }
        if (StringUtils.endsWith("in", site2)) {
            ChannelHandlerContext ctx = NettyCtxMap.get("in");
            if (ctx == null) {
                if (this.nettyClient.getSensor4EventLoopGroup() != null) {
                    this.nettyClient.shutdownKndJK();
                }
                boolean isConn = this.nettyClient.runKndJK();
                if (!isConn) {
                    this.nettyClient.shutdownKndJK();
                    log.error("knd-jk的tcp连接异常");
                    result.put("msg", (Object) "进口继电器连接异常");
                    result.put(BindTag.STATUS_VARIABLE_NAME, (Object) false);
                    return result;
                }
            }
            try {
                ThreadUtils.sleep(Duration.ofMillis(500L));
            } catch (InterruptedException e) {
                log.error(e.toString());
            }
            boolean status = sendCommand(site2, order2);
            result.put(BindTag.STATUS_VARIABLE_NAME, (Object) Boolean.valueOf(status));
            return result;
        }
        if (StringUtils.endsWith("out", site2)) {
            ChannelHandlerContext ctx2 = NettyCtxMap.get("out");
            if (ctx2 == null) {
                if (this.nettyClient.getSensor5EventLoopGroup() != null) {
                    this.nettyClient.shutdownKndCK();
                }
                boolean isConn2 = this.nettyClient.runKndCK();
                if (!isConn2) {
                    this.nettyClient.shutdownKndCK();
                    log.error("knd-ck的tcp连接异常");
                    result.put("msg", (Object) "出口继电器连接异常");
                    result.put(BindTag.STATUS_VARIABLE_NAME, (Object) false);
                    return result;
                }
            }
            try {
                ThreadUtils.sleep(Duration.ofMillis(500L));
            } catch (InterruptedException e2) {
                log.error(e2.toString());
            }
            boolean status2 = sendCommand(site2, order2);
            result.put(BindTag.STATUS_VARIABLE_NAME, (Object) Boolean.valueOf(status2));
            return result;
        }
        result.put("msg", (Object) "点位信息错误");
        result.put(BindTag.STATUS_VARIABLE_NAME, (Object) false);
        return result;
    }
}